﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DefaultRoom : Room
{
    public Text RoomNameText;
    public Text RoomIdText;
    public Text MemberCountText;

    /// <summary>
    /// 是否第一次点击
    /// </summary>
    private bool isFirstPressed = true;
    /// <summary>
    /// 是否可以执行检测
    /// </summary>
    private bool checkOK = false;
    /// <summary>
    /// 第一次单击时间
    /// </summary>
    private float firstPressedTime;
    /// <summary>
    /// 第二次单击时间
    /// </summary>
    private float secondPressedTime;
    /// <summary>
    /// 两次单击间隔
    /// </summary>
    private float pressInterval = 0.3f;
    private event Action OnDoubleClick;

    public void Awake()
    {
        GetComponent<Button>().onClick.AddListener(() => {
            if (isFirstPressed)
            {
                firstPressedTime = Time.time;
                isFirstPressed = false;
            }
            else
            {
                secondPressedTime = Time.time;
                isFirstPressed = true;
                checkOK = true;
            }
        });
        OnDoubleClick += () => {
            Debug.Log(Name + "执行双击事件!");
            Singleton<RoomManager>.GetInstance().Remove(Id);    // 移除房间
            CtlMsgHandle.GetRoomList();     // 重新获取房间列表
        };
    }
    public void Update()
    {
        RoomNameText.text = Name;
        RoomIdText.text = Id;
        MemberCountText.text = MemberCount.ToString();

        CheckDoubleClick();
    }
    public void CheckDoubleClick()
    {
        if(checkOK)
        {
            if (secondPressedTime - firstPressedTime <= pressInterval)
                OnDoubleClick();
            checkOK = false;
        }
    }
}
